# encoding: utf-8


#Interface Graphique temporaire: elle permettra de choisir les controleurs MIDI et faire un mix du signal.
import wx
from srvr import *
from pyolib._wxwidgets import VuMeter, ControlSlider, BACKGROUND_COLOUR
COLOURS = ["#000000", "#FF0000", "#0000FF", "#00FF00", "#FF00FF", "#FFFF00", "#00FFFF"]

class MainFrame(wx.Frame):
    def __init__(self, parent, id=-1, title="ELE", pos=(260,260), size=(526,130)):
        wx.Frame.__init__(self, parent, id, title, pos, size)
        self.SetMinSize((526, 130))
        self.path = ""

        self.sndProcessing = ["delay", "distortion", "freq shift"]
        
        self.audioServer = Audio_Server()
        self.fxs= MFX()

        menubar = wx.MenuBar()
        fileMenu = wx.Menu()

        self.sndProcess = wx.Menu()
        for i, sp in enumerate(self.sndProcessing):
            self.sndProcess.Append(i+9000, sp, kind=wx.ITEM_CHECK)
            self.Bind(wx.EVT_MENU, self.addModule, id=i+9000)
        fileMenu.AppendMenu(-1, "FX", self.sndProcess)
        fileMenu.AppendSeparator()

  
  
        fileMenu.Append(wx.ID_EXIT, "Quit\tCtrl+Q")
        menubar.Append(fileMenu, "File")

        audioMenu= wx.Menu()
        #midiMenu= wx.Menu()

        audioMenu.Append(10000, "Start\tCtrl+.", kind=wx.ITEM_CHECK)
        audioMenu.AppendSeparator()
        self.names, default = self.audioServer.getAudioDriverNames()
        for i, name in enumerate(self.names):
            audioMenu.Append(i+9000, name, kind=wx.ITEM_RADIO)
            if name == default:
                audioMenu.Check(i+9000, True)
            self.Bind(wx.EVT_MENU, self.changeAudioDriver, id=i+9000)
        menubar.Append(audioMenu, "Audio")

        ############################################


        #############################################

        self.SetMenuBar(menubar)

        self.Bind(wx.EVT_MENU, self.onQuit, id=wx.ID_EXIT)
        self.Bind(wx.EVT_CLOSE, self.onQuit)
        self.Bind(wx.EVT_MENU, self.onStartStop, id=10000)
        
        self.panel = wx.Panel(self)
        self.panel.SetBackgroundColour("#000000")
        self.box = wx.BoxSizer(wx.VERTICAL)

        self.controlPanel = ControlPanel(self.panel, self.audioServer, self.fxs)#self.audioServer
        self.box.Add(self.controlPanel, 0, wx.EXPAND|wx.ALL, 5)
        
        self.panel.SetSizer(self.box)


        self.Show()
    def getStateDictionary(self):
        dict = {}
        sndProcessing = self.audioServer.getModules()
        names = [sndProcessing.name for module in sndProcessing]
        states = [sndProcessing.getState() for sp in modules]
        dict["sndProcessing"] = names
        dict["states"] = states
        return dict

    def setStateDictionary(self, dict):
        names = dict["sndProcessing"]
        states = dict["states"]
        for i in range(len(names)):
            self.addModuleFromName(names[i], states[i])



    def onStartStop(self, evt):
        if evt.GetInt() == 1:
            self.audioServer.start()
        else:
            self.audioServer.stop()

    def changeAudioDriver(self, evt): #***
        name = self.names[evt.GetId()-9000]
        self.audioServer.changeAudioDriver(name)

    
    def addModule(self, evt):
        name = self.sndProcessing[evt.GetId()]
        obj = self.s.addSndProcessing(name)
        panel = DAV_Panel(self.panel, obj=obj)
        self.box.Add(panel, 1, wx.EXPAND|wx.ALL, 10)
        self.box.Layout()
        self.Refresh()

    def addModuleFromName(self, name, state):
        obj = self.audioServer.addSndProcessing(name)
        obj.setState(state)
        panel = TrajPanel(self.panel, obj=obj)#change to mixer,fader
        self.box.Add(panel, 1, wx.EXPAND|wx.ALL, 10)
        self.box.Layout()
        self.Refresh()
        


    def onQuit(self, evt):
        self.audioServer.stop()
        self.Destroy()

#ajouter le choix des effet sur le panneu. ainsi que le routage. 
class ControlPanel(wx.Panel):
    def __init__(self, parent, audioServer, fxs):
        wx.Panel.__init__(self, parent)
        self.SetBackgroundColour("#000000")
        self.audioServer= audioServer   
        self.fxs=fxs

        self.box = wx.BoxSizer(wx.HORIZONTAL)

        # Boite verticale pour le gain (label en haut et slider en bas)
        self.gainBox = wx.BoxSizer(wx.VERTICAL)  
        #self.gainBox.SetBackgroundColour("#000000")  #    
        self.masterText = wx.StaticText(self, label="Master Gain (dB)")
        self.masterText.SetForegroundColour((255,255,255))
        
        self.masterGain = ControlSlider(self, -60, 6, init=0, outFunction=self.setMasterGain)
        self.gainBox.Add(self.masterText, 1)
        self.gainBox.Add(self.masterGain, 1, wx.EXPAND)
        

        self.SetSizer(self.box)
###############################################################
        # DELAY:Boite verticale pour le gain de delay (label en haut et slider en bas)
        self.delaygainBox = wx.BoxSizer(wx.VERTICAL)  
        #self.gainBox.SetBackgroundColour("#000000")  #    
        self.delayText = wx.StaticText(self, label="Delay Gain (dB)")
        self.delayText.SetForegroundColour((255,255,255))
        
        self.delayGain = ControlSlider(self, -90, 9, init=0, outFunction=self.setDelayGain)#controlador
        self.delaygainBox.Add(self.delayText, 2)
        self.gainBox.Add(self.delayGain, 1, wx.EXPAND)
        

        self.SetSizer(self.box)
#####################################################################
    #connecter fader au setAmp du mixer

        #**********vuMetre*******************************
        self.meter = VuMeter(self, numSliders=2)
        # connection du Vu with Server
        self.audioServer.s._server.setAmpCallable(self.meter)
        
        self._fx1 = VuMeter(self, numSliders=6)
        self.fxs._mix.setAmp(0,1,0.78)

        
        self.box.Add(self.gainBox, 1, wx.EXPAND|wx.ALL, 10)
        self.box.Add(self.meter, 0, wx.TOP, 20)
        self.box.Add(self._fx1, 2, wx.BOTTOM, 10)
        
    def setMasterGain(self, x):
        x = pow(10, x * 0.05)
        #modifie l<amplitude globale du serveur audio
        self.audioServer.s.setAmp(x)

    def setDelayGain(self, x):
        x = pow(10, x * 0.05)
        #modifie l<amplitude globale du serveur audio
        self.fxs._mix.setAmp(0, 0, x)
